const {db} = require("../util/util")

const addUserToEvent = (user_id,event_id) => {//将用户加入到该投票事件
  const sql = "insert into user_event (user_id,event_id) values(?,?)";
  return db.execute(sql,[user_id,event_id]).then((data) => {
    return data[0].insertId;//通过判断插入后的id是否存在 来判断插入数据是否成功
  })
}

const updateIsVotedAndBoardId = (user_id,event_id,board_id) => {//更新是否投票字段 和 投给哪个候选人的board_id
  sql = "update user_event set is_voted = 1,board_id = ? where user_id = ? and event_id = ?";
  return db.execute(sql,[board_id,user_id,event_id]).then((data) => {
    return data[0].affectedRows;//判断是否更新成功
  })
}

const deleteByUserId = (user_id) => {//删除用户参与的所有事件的绑定记录
  console.log("22222222********",user_id)
  sql = " delete from user_event where user_id = ?";
  return db.execute(sql,[user_id]).then((data) => {
    return data[0].affectedRows;
  })
}


module.exports = {addUserToEvent, updateIsVotedAndBoardId, deleteByUserId}

// deleteByUserId(10)